package ad.ui 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import com.greensock.easing.Expo;
	import com.greensock.TweenMax;
	
	/**
	 * Combobox
	 * @version ActionScript 3.0
	 * @author Adrian C. Miranda
	 * @mail ad@adrianmiranda.com.br
	 */
	public class Combobox extends Sprite
	{
		protected var list:List;
		protected var isOpen:Boolean;
		
		public function Combobox(list:Array = null) 
		{
			list = new List(list);
			list.addEventListener(Event.SELECT, onItemListSelect);
			addChild(list);
		}
		
		protected /*override */function initialize():void 
		{
			addEventListener(MouseEvent.CLICK, toggleClose);
		}
		
		protected /*override */function finalize():void 
		{
			
		}
		
		protected function draw():void 
		{
			
		}
		
		public function addItem(item:Object):void 
		{
			list.addItem(item);
		}
		
		public function addItemAt(item:Object, index:int):void 
		{
			list.addItemAt(item, index);
		}
		
		public function removeItem(item:Object):void 
		{
			list.removeItem(item);
		}
		
		public function removeItemAt(index:int):void 
		{
			list.removeItemAt(index);
		}
		
		public function removeAll():void 
		{
			list.removeAll();
		}
		
		public function get selected():void 
		{
			return list.selected;
		}
		
		private function onItemListSelect(evt:Event):void 
		{
			toggleClose();
			dispatchEvent(evt.clone());
		}
		
		private function toggleClose(evt:MouseEvent = null):void 
		{
			if (isOpen) 
			{
				TweenMax.to(list, 0.6, { y:0, ease:Expo.easeOut } );
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, update);
			}
			else 
			{
				TweenMax.to(list, 0.6, { y:0, ease:Expo.easeOut } );
				stage.addEventListener(MouseEvent.MOUSE_MOVE, update);
			}
			isOpen = !isOpen;
		}
		
		private function update(evt:Event):void 
		{
			//if (mouseX < 0 || mouseX > width || mouseY < 0 || mouseY > ((list.height + contHeight) * 1.2))
			//{
				toggleClose();
			//}
		}
		
	}

}